package LeetCode;

import java.util.Stack;

public class LC946 {
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        if(pushed==null||pushed.length ==0) return true;
        if(pushed.length!=popped.length) return false;
        int N = popped.length;
        Stack<Integer> stack = new Stack<>();
        int j = 0;
        for (int x: pushed) {
            stack.push(x);
            while (!stack.isEmpty() && j < N && stack.peek() == popped[j]) {
                stack.pop();
                j++;
            }
        }
        return j == N;
    }
}